﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GibberDoc.Classes.UniTech
{
	public class RandomParagraphGenerator
	{
        private string[]
            _templates,
			_docNames,
            _nouns,
            _verbs,
            _prefixes,
            _suffixes,
            _pronouns,
            _prepositions,
            _interjections,
            _determiners,
            _conjuctions,
            _adverbs,
            _adjectives,
            _modals,
            _places,
            _time;

		static Random _random = new Random();
		StringBuilder _builder;

		public RandomParagraphGenerator(bool business = false)
		{
			_builder = new StringBuilder();
            _templates = LoadWordFile("templates");
			_docNames = LoadWordFile("business-nouns-filename");
            if (business)
            {
                _nouns = LoadWordFile("business-nouns");
            }
            else
            {
                _nouns = LoadWordFile("nouns");
            }
            _verbs = LoadWordFile("verbs");
            _prefixes = LoadWordFile("prefixes");
            _suffixes = LoadWordFile("suffixes");
            _pronouns = LoadWordFile("pronouns");
            _prepositions = LoadWordFile("prepositions");
            _interjections = LoadWordFile("interjections");
            _determiners = LoadWordFile("determiners");
            _conjuctions = LoadWordFile("conjuctions");
            _adverbs = LoadWordFile("adverbs");
            _adjectives = LoadWordFile("adjectives");
            _modals = LoadWordFile("modals");
            _places = LoadWordFile("places");
            _time = LoadWordFile("time");
		}

		private string[] LoadWordFile(string filename)
        {
			// Display the file contents to the console. Variable text is a string.
			string[] lines = System.IO.File.ReadAllLines(@"Resources\" + filename + ".txt");

            return lines;
        }

		public void AddContentParagraphs(int numberParagraphs, int minSentences, int maxSentences)
		{
			for (int i = 0; i < numberParagraphs; i++)
			{
					AddParagraph(_random.Next(minSentences, maxSentences + 1));
					_builder.Append("\n\n");
			}
		}

		void AddParagraph(int numberSentences)
		{
			for (int i = 0; i < numberSentences; i++)
			{
				AddSentence();
			}
		}

		void AddSentence()
		{
			StringBuilder b = new StringBuilder();
            string template = _templates[_random.Next(_templates.Length)];
			List<String> sentenceList = new List<string>();

			string terminator = ". ";

			foreach(string wordType in template.Split('-'))
			{
				switch(wordType)
                {
					case "noun":
                        sentenceList.Add(_nouns[_random.Next(_nouns.Length)]);
                        break;
                    case "object":
                        sentenceList.Add(_nouns[_random.Next(_nouns.Length)]);
                        break;
                    case "verb":
                        sentenceList.Add(_verbs[_random.Next(_verbs.Length)].Split(';')[1]);
						break;
                    case "prefix":
                        sentenceList.Add(_prefixes[_random.Next(_prefixes.Length)]);
						break;
                    case "suffix":
                        sentenceList.Add(_suffixes[_random.Next(_suffixes.Length)]);
						break;
                    case "pronoun":
                        sentenceList.Add(_pronouns[_random.Next(_pronouns.Length)]);
						break;
                    case "preposition":
                        sentenceList.Add(_prepositions[_random.Next(_prepositions.Length)]);
                        terminator = "? ";
						break;
                    case "interjection":
                        sentenceList.Add(_interjections[_random.Next(_interjections.Length)]);
						break;
                    case "determiner":
                        sentenceList.Add(_determiners[_random.Next(_determiners.Length)]);
						break;
                    case "conjuction":
                        sentenceList.Add(_conjuctions[_random.Next(_conjuctions.Length)]);
						break;
                    case "adverb":
                        sentenceList.Add(_adverbs[_random.Next(_adverbs.Length)]);
						break;
                    case "advective":
                        sentenceList.Add(_adjectives[_random.Next(_adjectives.Length)]);
						break;
                    case "modal":
                        sentenceList.Add(_modals[_random.Next(_modals.Length)]);
						break;
                    case "place":
                        sentenceList.Add(_places[_random.Next(_places.Length)]);
						break;
                    case "time":
                        sentenceList.Add(_time[_random.Next(_time.Length)]);
                        break;
					default:
                        sentenceList.Add(wordType);
                        break;
                }
            }
			// Add n words together.
                
			string sentence = string.Join(" ", sentenceList.ToArray()) + terminator;
			// Uppercase sentence
			sentence = char.ToUpper(sentence[0]) + sentence.Substring(1);

			// Add this sentence to the class
			_builder.Append(sentence);
		}
		public void ClearBuilder()
        {
			_builder.Clear();
        }

		public string Content
		{
			get
			{
				return _builder.ToString();
			}
		}
        public string DocName
        {
            get
            {
                return _prefixes[_random.Next(_prefixes.Length)] + " " + _docNames[_random.Next(_docNames.Length)] + " " + _suffixes[_random.Next(_suffixes.Length)];
            }
        }
	}
}
